/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.orderQuestion;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;

import ru.irenproject.AbstractQuestionEditor;
import ru.irenproject.Action;
import ru.irenproject.Check;
import ru.irenproject.Question;
import ru.irenproject.TestInputException;
import ru.irenproject.orderQuestion.OrderQuestionProtos.OrderQuestionEditorShape;
import ru.irenproject.orderQuestion.OrderQuestionProtos.Page;
import ru.irenproject.pad.Pad;
import ru.irenproject.pad.PadEditor;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;
import ru.irenproject.store.Thing;

import com.google.protobuf.Message;

public final class OrderQuestionEditor extends AbstractQuestionEditor {
  public static final class SelectPageInput {
    public Page page;
  }

  public static final class DeleteElementInput {
    public int index;
  }

  public static final class MoveElementInput {
    public int index;
    public boolean forward;
  }

  public static final class SetOptionsInput {
    public Integer itemLimit;
    public Integer distractorLimit;
  }

  @Stash private OrderQuestionProtos.OrderQuestionEditor.Builder $;

  @Inject OrderQuestionEditor() {}

  @Override public OrderQuestion question() {
    return get($.getQuestion(), OrderQuestion.class);
  }

  @Override protected void setQuestion(Question question) {
    Validate.isInstanceOf(OrderQuestion.class, question);

    $.setQuestion(question.id());
    $.setFormulationEditor(PadEditor.create(question().formulation()).id());
    $.setPage(Page.ITEMS);
    syncEditors();
    question().addListener(this);
  }

  private void syncEditors() {
    $.clearItemEditor();
    for (Pad pad : question().itemList()) {
      $.addItemEditor(PadEditor.open(pad).id());
    }

    $.clearDistractorEditor();
    for (Pad pad : question().distractorList()) {
      $.addDistractorEditor(PadEditor.open(pad).id());
    }
  }

  @Override protected void handleEvent(Event e, Thing source) {
    if (e instanceof OrderQuestion.ChangeEvent) {
      syncEditors();
      markForRender();
    }
  }

  @Override public Message render() {
    OrderQuestion q = question();
    OrderQuestionEditorShape.Builder b = OrderQuestionEditorShape.newBuilder()
        .setFormulationEditor($.getFormulationEditor())
        .setPage(page())
        .setItemCount(q.itemCount())
        .setDistractorCount(q.distractorCount())
        .setMaxElements(OrderQuestion.MAX_ELEMENTS)
        .setHasDefaultOptions(q.hasDefaultOptions());
    switch (page()) {
      case ITEMS: {
        b.addAllElementEditor($.getItemEditorList());
        break;
      }
      case DISTRACTORS: {
        b.addAllElementEditor($.getDistractorEditorList());
        break;
      }
    }

    if (q.hasItemLimit()) {
      b.setItemLimit(q.itemLimit());
    }
    if (q.hasDistractorLimit()) {
      b.setDistractorLimit(q.distractorLimit());
    }

    return b.build();
  }

  private Page page() {
    return $.getPage();
  }

  @Action(markAsModified = false)
  public void doSelectPage(SelectPageInput in) {
    $.setPage(Check.inputNotNull(in.page));
  }

  @Action(badInput = TestInputException.class)
  public void doAddElement() {
    switch (page()) {
      case ITEMS: {
        question().addItem();
        break;
      }
      case DISTRACTORS: {
        question().addDistractor();
        break;
      }
    }
  }

  @Action(badInput = TestInputException.class)
  public void doDeleteElement(DeleteElementInput in) {
    switch (page()) {
      case ITEMS: {
        question().deleteItem(in.index);
        break;
      }
      case DISTRACTORS: {
        question().deleteDistractor(in.index);
        break;
      }
    }
  }

  @Action(badInput = TestInputException.class)
  public void doMoveElement(MoveElementInput in) {
    switch (page()) {
      case ITEMS: {
        question().moveItem(in.index, in.forward);
        break;
      }
      case DISTRACTORS: {
        question().moveDistractor(in.index, in.forward);
        break;
      }
    }
  }

  @Action(badInput = TestInputException.class)
  public void doSetOptions(SetOptionsInput in) {
    if (in.itemLimit == null) {
      question().clearItemLimit();
    } else {
      question().setItemLimit(in.itemLimit);
    }

    if (in.distractorLimit == null) {
      question().clearDistractorLimit();
    } else {
      question().setDistractorLimit(in.distractorLimit);
    }
  }
}
